package com.leyou.item.controller;

import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.pojo.TbCategory;
import com.leyou.item.service.TbCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: guojy
 * @date: 2020/2/29 15:39
 * @Description: ${TODO}
 * @version:
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private TbCategoryService tbCategoryService;

    /**
     * 搜索分类管理数据
     *
     * @param pid 一级目录id
     * @return ResponseEntity
     */
    @GetMapping(value = "/of/parent")
    public ResponseEntity<List<CategoryDTO>> findByParentId(@RequestParam(name = "pid") Long pid) {
        List<CategoryDTO> CategoryDTOs = tbCategoryService.findByParentId(pid);
        return ResponseEntity.ok(CategoryDTOs);
    }

    /**
     * 根据品牌id查询中间表中的 分类id，然后查询分类
     * http://api.leyou.com/api/item/category/of/brand/?id=325400
     *
     * @param brandId 品牌id
     */
    @GetMapping(value = "/of/brand")
    public ResponseEntity<List<CategoryDTO>> findCategoryByBrandId(@RequestParam(name = "id") Long brandId) {
        // 查询数据
        List<TbCategory> list = tbCategoryService.selectCateByBrandid(brandId);
        // 封装DTO
        List<CategoryDTO> result = BeanHelper.copyWithCollection(list, CategoryDTO.class);
        return ResponseEntity.ok(result);
    }

    /**
     * TODO 根据商品分类id，查询商品分类，。需要一个根据多级分类id查询分类的接口
     * 用于搜索时 拼接 goods的 all字段
     *
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> findCategoryByIds(@RequestParam(name = "ids") List<Long> ids) {
        List<CategoryDTO> list = tbCategoryService.findByIds(ids);
        return ResponseEntity.ok(list);
    }


}
